<?php
// +----------------------------------------------------------------------
// | Description: 商品属性管理
// +----------------------------------------------------------------------
// | Author: orchief
// +----------------------------------------------------------------------
// | Date: 2018-01-24 16:43:34
// +----------------------------------------------------------------------

namespace app\client\controller;

use app\common\controller\Rest;
use think\Db;

class ProductAttrs extends Rest
{
    public $modelName = 'ProductAttr';
    public function _initialize()
    {
        parent::_initialize();
        // $this -> abort(!$this -> isClient, 'Permission denied', 403);
    }

    public function index()
    {
        $data = self::$model->getDataList();
        $filterData = $data['list'];
        $AttributeId = [];
        foreach($filterData as $k => $v){
            if(in_array($v['AttributeId'], $AttributeId)){
                $key = array_search($v['AttributeId'], $AttributeId);
                $list[$key]['AttributeList'][] = [
                    'ValueId'    =>  $v['ValueId'],
                    'ValueStr'   =>  $v['ValueStr'],
                    'IsImg'      =>  $v['IsImg'],
                ];
            }else{
                $AttributeId[] = $v['AttributeId'];
                $temp = [];
                $temp['ProductId'] = $v['ProductId'];
                $temp['AttributeId'] = $v['AttributeId'];
                $temp['AttributeName'] = $v['AttributeName'];
                $temp['Id'] = $v['Id'];
                $temp['SortBy'] = $v['SortBy'];
                $temp['IsSku'] = $v['IsSku'];
                $temp['AttributeList'][] = [
                    'ValueId'    =>  $v['ValueId'],
                    'ValueStr'   =>  $v['ValueStr'],
                    'IsImg'      =>  $v['IsImg'],
                ];
                $list[] = $temp;
            }
        }
        $data['list'] = $list;
        $this->jsonData($data, self::$model -> getError());
    }


    /**
     * Cartesian product that generates attribute values and attributes
     *
     * @return void
     */
    public function genSku()
    {
        // TODO: process Cartesian product
        $params = $this->param;
        if($params['IsSku'] == 1){
            self::$model -> startTrans();
            try{
                
                // attributes
                $attributes = Db::table('shop_goods_attributes');
                $attributes -> insert(['AttributeName' => $params['AttributeName']]);
                $params['AttributeId'] = $attributes -> getLastInsId();

                // attribute_values
                $attribute_values = Db::table('shop_goods_attribute_values');
                foreach($params['AttributeValueArray'] as $k => $v){
                    // $attribute_values -> insert();
                    $attr[] = ['AttributeId' => $params['AttributeId'], 'ValueStr' => $v ];
                    $params['ValueId'] = $attribute_values -> getLastInsId();
                }

                $attribute_values -> insertAll($attr);
                $res = self::$model->createData($params);

                // TODO 生成规格的笛卡尔积
                

                self::$model -> commit();
            }catch(\Exception $e){
                self::$model -> rollback();
                $this -> abort(false, $e -> getMessage());
            }
            $this -> checkRes($res, '添加成功', self::$model -> getError());

        }else{
            self::$model -> startTrans();
            try{
                 // attributes
                $attributes = Db::table('shop_goods_attributes');
                $attributes -> insert(['AttributeName' => $params['AttributeName']]);
                $params['AttributeId'] = $attributes -> getLastInsId();

                // attribute_values
                $attribute_values = Db::table('shop_goods_attribute_values');
                $attribute_values -> insert(['AttributeId' => $params['AttributeId'], 'ValueStr' => $params['AttributeValueArray'][0] ]);
                $params['ValueId'] = $attribute_values -> getLastInsId();

                // p($params);

                $res = self::$model->createData($params);

                self::$model -> commit();
            }catch(\Exception $e){
                self::$model -> rollback();
                $this -> abort(false, $e -> getMessage());
            }
            $this -> checkRes($res, '添加成功', self::$model -> getError());
        }

        // TODO: insert data to mysql
    }
}
